using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using FeedBurner.Utility;

namespace FeedBurner.Operations
{
    internal class FindFeeds
    {
        public List<Feed> Find(string username, string password)
        {
            string Query = string.Format("?user={0}&password={1}",
                username, password);
            string PureData = DataManager.Get(Urls.FindUrl() + Query);

            return GetFeeds(PureData);
        } 

        private List<Feed> GetFeeds(string Output)
        {
            List<Feed> Feeds = new List<Feed>();

            XmlDocument Xml = new XmlDocument();
            Xml.LoadXml(Output);

            if (Errors.CheckError(Xml))
            {
                XmlNodeList Nodes =
                    Xml.SelectNodes("//rsp/feeds/feed");

                if (Nodes != null)
                {
                    foreach (XmlNode Node in Nodes)
                    {
                        Feed Feed = new Feed();
                        Feed.ID = Convert.ToInt64
                            (Node.Attributes["id"].InnerText);
                        Feed.Title = Node.Attributes["title"].InnerText;
                        Feed.Uri = Node.Attributes["uri"].InnerText;
                        Feeds.Add(Feed);
                    }
                }
                return Feeds;
            }
            return null;
        }
    }
}
